package main.java;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import org.apache.log4j.Logger;
import org.xml.sax.SAXException;

import model.entities.UserCode;

public class Main {

	private static Logger logger = Logger.getLogger(Main.class);

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException
	{
		Hsqldb hsqlServer = new Hsqldb();
		hsqlServer.startHsqlDB();

		GSI2LandXML converter = new GSI2LandXML();
		ArrayList<String> files = new ArrayList<String>();

		// Test set 1
		files.add("files/dp_01.gsi");
		files.add("files/dp_01_coor.gsi");

		convertDocuments(converter, files);

		// Validate output file of test set 1
		File xmlOutput = new File(converter.getXmlOutputPath());

		boolean xmlValid = false;
		try {
			xmlValid = converter.validateLandXML(xmlOutput);
			System.out.println("Valid XML File: " + xmlValid);
		} catch (SAXException e) {
			System.out.println("SAXException thrown. XML-Validation failed.");
			e.printStackTrace();
		}

		// Test set 2
		converter = new GSI2LandXML();
		files.clear();
		files.add("files/dp_02.gsi");
		files.add("files/dp_02_coor.gsi");

		convertDocuments(converter, files);

		// Validate output file of test set 2
		xmlOutput = new File(converter.getXmlOutputPath());

		xmlValid = false;
		try {
			xmlValid = converter.validateLandXML(xmlOutput);
			System.out.println("Valid XML File: " + xmlValid);
		} catch (SAXException e) {
			System.out.println("SAXException thrown. XML-Validation failed.");
			e.printStackTrace();
		}

		hsqlServer.closeHsqlDB();
	}

	public static void convertDocuments(GSI2LandXML converter, ArrayList<String> files) throws IOException
	{
		logger.debug("Starting conversion...");
		converter.convertGSI2LandXML(files);
		logger.debug("Conversion finished");
	}

	public static void addUserDefinitions(String code, String wi, String label, String target)
	{
		UserCode uc = new UserCode();
		uc.setCode(Integer.parseInt(code));
		uc.setWi(Integer.parseInt(wi));
		uc.setLabel(label);
		uc.setTarget(target);

		DAO dao = new DAO();
		dao.addUserCode(uc);
	}
}
